<template>
	<view>
		<view class="goods-list">
			<block v-for="(goods,index) in goodsList" :key="index">
				<my-goods :goods="goods" @click.native="gotoDetail(goods)"></my-goods>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList() //	获取商品列表
		},
		onReachBottom() { //	下拉触底事件
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
				return uni.$showMsg('数据已经全部加载完成')
			}
			if (this.isLoading) {
				return
			}
			this.getGoodsList()
			this.queryObj.pagenum += 1
		},
		onPullDownRefresh() { //	上拉刷新事件
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.goodsList = []
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		data() {
			return {
				queryObj: {
					query: '', //	查询关键词
					cid: '', //	商品分类id
					pagenum: 1, //	页码值
					pagesize: 15, //	每页显示多少条数据
				},
				goodsList: [], //	商品列表
				total: 0, //	总数量
				isLoading: false
			};
		},
		methods: {
			async getGoodsList(cb) { //	获取商品列表数据
				this.isLoading = true
				const res = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				this.isLoading = false
				if (res.statusCode !== 200) {
					return uni.$showMsg()
				}
				this.goodsList = [...this.goodsList, ...res.data.message.goods]
				this.total = res.data.message.total
				cb && cb()
			},
			gotoDetail(goods) { //	跳转到详情页面
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?goods_id=${goods.goods_id}`
				})
			}
		},
	}
</script>

<style lang="scss">
	.goods-list {
		background-color: white;


	}
</style>
